package com.xxjqr.rbac.controller;


import com.github.pagehelper.PageInfo;
import com.xxjqr.rbac.anno.OperateLog;
import com.xxjqr.rbac.auth.DynamicSecurityMetadataSource;
import com.xxjqr.rbac.auth.JwtTokenUtil;
import com.xxjqr.rbac.constant.Const;
import com.xxjqr.rbac.entity.dto.UserLoginDto;
import com.xxjqr.rbac.entity.dto.UserDto;
import com.xxjqr.rbac.entity.dto.UserPutDto;
import com.xxjqr.rbac.entity.vo.UserLoginVo;
import com.xxjqr.rbac.entity.vo.UserVo;
import com.xxjqr.rbac.enums.OperTypeEnum;
import com.xxjqr.rbac.enums.CodeMsgEnum;
import com.xxjqr.rbac.enums.OperatorTypeEnum;
import com.xxjqr.rbac.enums.StatusEnum;
import com.xxjqr.rbac.exception.CommonException;
import com.xxjqr.rbac.service.TbUserService;
import com.xxjqr.rbac.util.RedisUtil;
import lombok.AllArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 * @author 丁昌江
 * @since 2021-02-26
 */
@RestController
@RequestMapping("user")
@AllArgsConstructor
public class TbUserController {

    private final TbUserService userService;
    private final RedisUtil redisUtil;
    private final JwtTokenUtil jwtTokenUtil;
    private final DynamicSecurityMetadataSource dynamicSecurityMetadataSource;

    /**
     * @param name      用户姓名
     * @param mobile    用户手机号
     * @param status    用户状态
     * @param beginTime 查询开始时间
     * @param endTime   查询结束时间
     * @param deptIds   部门ids
     * @描述 获取用户信息
     * @返回 com.github.pagehelper.PageInfo<com.xxjqr.rbac.entity.vo.UserVo>
     * @码农 丁昌江
     * @日期 2021/3/4 17:19
     */
    @GetMapping("getUsers")
    public PageInfo<UserVo> getUsers(String name,
                                     String mobile,
                                     StatusEnum status,
                                     String beginTime,
                                     String endTime,
                                     @RequestParam(value = "deptIds",required = false) List<Long> deptIds) {
        return userService.getUsers(name, mobile, status, beginTime, endTime,deptIds);
    }

    /**
     * @param userDto
     * @描述 添加用户，使用 BCrypt对密码加密
     * @返回 java.lang.Boolean
     * @码农 丁昌江
     * @日期 2021/3/4 17:19
     */
    @PostMapping("addUser")
    @OperateLog(title = "用户管理",operType = OperTypeEnum.INSERT,operatorType = OperatorTypeEnum.MANAGE)
    public Boolean addUser(@RequestBody @Valid UserDto userDto) {
        return userService.addUser(userDto);
    }

    /**
     * @param id 用户id
     * @描述 删除用户
     * @返回 java.lang.Boolean
     * @码农 丁昌江
     * @日期 2021/3/4 17:20
     */
    @DeleteMapping("delUser")
    @OperateLog(title = "用户管理", operType = OperTypeEnum.DELETE, operatorType = OperatorTypeEnum.MANAGE)
    public Boolean delUser(@RequestParam Long id) {
        return userService.removeById(id);
    }

    /***
     * @描述 editUser
     * @码农 丁昌江
     * @日期 2021/5/13 9:47
     * @param userPutDto
     * @返回 java.lang.Boolean
     */
    @PutMapping("editUser")
    @OperateLog(title = "用户管理", operType = OperTypeEnum.UPDATE, operatorType = OperatorTypeEnum.MANAGE)
    public Boolean editUser(@RequestBody @Valid UserPutDto userPutDto) {
        return userService.editUser(userPutDto);
    }



    /**
     * @描述 login 用户登录
     * @返回 com.xxjqr.rbac.entity.vo.LoginUserVo
     * @码农 丁昌江
     * @日期 2021/3/9 14:13
     */
    @PostMapping("login")
    public UserLoginVo login(@RequestBody @Valid UserLoginDto loginDto, HttpServletRequest request) {
        //先校验验证码，从redis中取出，如果取不出则是验证码已过期
        String captcha = (String) redisUtil.hget(Const.CAPTCHA_SESSION_KEY, request.getSession().getId());
        if (!StringUtils.hasLength(captcha)) {
            throw CommonException.generate(CodeMsgEnum.CAPTCHA_EXPIRE);
        }
        //验证码不正确
        if (!captcha.equals(loginDto.getCaptcha())) {
            throw CommonException.generate(CodeMsgEnum.CAPTCHA_FAIL);
        }
        return userService.login(loginDto,request);
    }

    @PostMapping("logout")
    public void logout(HttpServletRequest request) {
        String token = request.getHeader("token");
        jwtTokenUtil.setTokenExpiration(token);
        dynamicSecurityMetadataSource.clearDataSource();
    }

}
